<style lang="less" scoped>
@import '../common/less/varible.less';

.playlist-search {
    // padding: @pad;
    box-sizing: border-box;

    .search-top {
        width: 100%;
        display: flex;
        padding: 10px 20px 10px 20px;

        .back {
            height: 40px;
            width: 50px;
            text-align: center;
            line-height: 40px;
            font-size: 18px;
            // font-family: Arial, Helvetica, sans-serif;
        }

        .inp {
            border: 1px solid #f50;
            height: 40px;
            padding-left: 15px;
            box-sizing: border-box;
            font-size: 16px;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
        }

        input[type='text']::placeholder {
            // color: @activeColor;
        }

        .btn {
            background-color: #f50;
            color: #ffffff;
            width: 60px;
            height: 40px;
            font-size: 16px;
            font-family: Arial, Helvetica, sans-serif;
        }
    }

    ul.song-list-1 {
        padding: 0 15px;
        margin-top: 10px;

        li {
            .opt {
                display: flex;
                width: 100%;
                justify-content: space-between;
                align-items: center;
                font-family: Arial, Helvetica, sans-serif;
                padding: 10px 0px;
                border-bottom: 1px solid #f0f0f0;

                .left {
                    display: flex;

                    .img-box {
                        width: 80px;
                        margin-right: 10px;

                        img {
                            width: 100%;
                        }
                    }

                    h5 {
                        font-weight: normal;
                        font-size: 18px;
                        line-height: 175%;
                    }

                    div {
                        font-size: 13px;
                        color: #666;

                        i {
                            display: inline-block;
                            width: 12px;
                            height: 8px;
                            margin-right: 4px;
                            background-image: url(../assets/img/index_icon_2x.png);
                            background-size: 166px 97px;
                        }
                    }
                }

                .right {
                    button {
                        background-color: transparent;
                        color: #ccc;
                    }
                }
            }
        }
    }

    .playlist-header {
        width: 100%;
        height: 50px;
        line-height: 50px;
        font-size: 18px;
        padding-left: 20px;
        color: #000;
        display: flex;
        align-items: center;
        position: sticky;
        top: 0;
        background-color: #fff;

        .van-icon {
            margin-right: 10px;
        }

        h3 {
            padding-left: 20px;
        }
    }

    .my-playlist {
        width: 100%;
        height: 50px;
        line-height: 50px;
        font-size: 18px;
        padding-left: 0px;
        color: #000;

        h3 {
            padding-left: 15px;
        }
    }
}
</style>

<template>
    <div class="playlist-search">
        <div class="playlist-header">
            <div class="back" @click="goBack">
                <!-- <van-icon name="revoke" /> -->
                返回
                <!-- <van-icon name="arrow-left" /> -->
            </div>
            <h3>歌单歌曲列表</h3>
        </div>
        <!-- <h3>搜索</h3> -->
        <div class="search-top">
            <input class="inp" @keydown.enter="getInputValue" v-model="text" type="text" placeholder="关键字搜歌">
            <button class="btn" @click="getInputValue">搜索</button>
        </div>
        <div v-if="isLoading">
            <!-- 使用loading组件 -->
            <my-loading></my-loading>
        </div>

        <div v-else>
            <div v-if="arr.length == 0">
                <!-- 搜索反馈 -->
                <!-- <van-empty description="暂无搜索列表" /> -->
            </div>
            <!-- 搜索列表 -->
            <ul class="song-list-1" v-else>
                <li v-for="(item) in arr" :key="item.id">
                    <!-- <router-link :to="{ path: '/detail', query: { song_id: item.song_id , ui:'search',text: text} }"> -->
                    <div class="opt">
                        <div class="left">
                            <div class="img-box" style="width: 60px; height: 60px;"><img v-if="item.song_cover_url"
                                    :src="item.song_cover_url" alt="">
                            </div>
                            <div>
                                <h5>{{ item.song_name }}</h5>
                                <div v-if="item.singer_name">
                                    <span>{{ item?.singer_name }} </span> |
                                    <span>{{ item?.category_name }} </span>
                                </div>
                            </div>
                        </div>
                        <div class="right">
                            <!-- 添加歌单歌曲 -->
                            <button
                                @click="addPlayListSongFn(playlist_id, item.song_id, item.song_name, item.src2, item.singer_name, item.song_cover_url2, item.lyrics)">
                                <van-icon name="add-o" />
                            </button>
                            <!-- 播放歌曲 -->
                            <button @click="playSongFn(item.song_id, item.song_name)">
                                <van-icon name="play-circle-o" />
                            </button>
                        </div>
                    </div>
                    <!-- </router-link> -->
                </li>
            </ul>
        </div>


        <div class="my-playlist">
            <h3>{{ playlist_name }}</h3>
            <!-- 歌单列表 -->
            <ul class="song-list-1">
                <li v-for="(item) in arr2" :key="item.id">
                    <!-- <router-link :to="{ path: '/detail', query: { song_id: item.song_id , ui:'search',text: text} }"> -->
                    <div class="opt">
                        <div class="left" @click="toPlayListSongFn(item.song_id, item.song_name)">
                            <!-- 播放歌曲 -->
                            <!-- <van-icon name="play-circle-o" @click="toPlayListSongFn(item.song_id, item.song_name)" /> -->
                            <div class="img-box" style="width: 60px; height: 60px;"><img style="border-radius: 10px;"
                                    v-if="item.song_cover_url" :src="item.song_cover_url" alt="">
                            </div>
                            <div>
                                <h5>{{ item.song_name }}</h5>
                                <div>{{ item.singer_name }}</div>
                                <!-- <div> <span>{{ item?.add_time | formatime }} </span> </div> -->
                            </div>
                        </div>
                        <div class="right">
                            <!-- 移除歌单歌曲 -->
                            <!-- <van-icon name="minus" @click="removePlayListSongFn(item.id)"/> -->
                            <span style="color: #666; font-size: 15px;" @click="removePlayListSongFn(item.id)">移除</span>
                            <!-- <button > 移除</button> -->
                            &nbsp;&nbsp;

                            <!-- <button > </button> -->
                        </div>
                    </div>
                    <!-- </router-link> -->
                </li>
            </ul>
            <div v-if="arr2.length == 0">
                <!-- 搜索反馈 -->
                <van-empty description="暂无歌单歌曲列表" />
            </div>
        </div>

    </div>
</template>


<script>
// 导入组件
import { Toast } from 'vant'
import moment from 'moment'
// 导入自定义组件
import MyLoading from '../components/MyLoading.vue'
// 导入接口
import { addPlayListSong, removePlayListSong } from '../api/index'
export default {
    // 注册组件
    components: {
        MyLoading,
    },
    filters: {
        formatime(value) {
            // console.log(moment(new Date()).format("YY-MM-DD hh:mm:ss"))
            return moment(new Date(value)).format("YY-MM-DD hh:mm:ss");
        }
    },
    data() {
        return {
            arr: [],
            arr2: [],
            text: "", // 记录输入框的值,
            isLoading: false,
            playlist_name: "",
            playlist_id: "",
        }
    },
    created() {
        let query = this.$route.query
        console.log(query.playList_name)
        if (query.playList_name) {
            this.playlist_name = query.playList_name;
            this.playlist_id = query.playlist_id;
            this.allPlayListSongFunc(query.playlist_id);
        }

    },

    methods: {
        // 删除歌单歌曲
        async removePlayListSongFn(id) {
            await removePlayListSong({ id }).then(
                content => {
                    console.log(content)
                    if (content.code == 200) {
                        this.allPlayListSongFunc(this.playlist_id);
                    }
                    Toast(content.msg);
                }
            ).catch(err => console.log(err))
        },
        // 添加歌单歌曲
        async addPlayListSongFn(playlist_id, song_id, song_name, src, singer_name, song_cover_url, lyrics) {
            let add_time = moment(new Date()).format("YY-MM-DD hh:mm:ss")
            await addPlayListSong({ playlist_id, song_id, song_name, add_time, src, singer_name, song_cover_url, lyrics }).then(
                content => {
                    console.log(content)
                    if (content.code == 200) {
                        this.allPlayListSongFunc(this.playlist_id);
                    }
                    Toast(content.msg);
                }
            ).catch(err => console.log(err))
        },
        // 跳转播放
        playSongFn(song_id, text) {
            this.$router.push({ path: '/detail', query: { song_id: song_id, ui: 'search', text: text } })
        },
        toPlayListSongFn(song_id, text) {
            this.$router.push({ path: '/detail', query: { song_id: song_id, ui: 'playlist', text: text, playlist_id: this.playlist_id } })
        },
        // 获取歌单歌曲
        async allPlayListSongFunc(playList_id) {
            await this.$store.dispatch('music/allPlayListSongFn', { playList_id: playList_id });
            this.arr2 = this.$store.state.music.songs;
        },
        // 自定义方法
        async songSearchFn(value) {
            // console.log({keyword: value})
            // 请求之前 显示loading
            this.isLoading = true;
            await this.$store.dispatch('music/searchSongFn', { keyword: value });
            this.arr = this.$store.state.music.songs;
            // 请求之后
            setTimeout(() => {
                // 隐藏loading
                this.isLoading = false;
            }, 500);
        },

        // 获取输入框输入的值
        getInputValue() {
            // 获取输入框的值
            let text = this.text;
            // 判断输入框是否为空
            if (text == "" || text.length == 0) {
                Toast("请输入关键字");
                return false;
            }

            // 检查代码是否执行到此处
            // console.log({text});

            // 搜素歌曲
            this.songSearchFn(text);
        },

        // 返回上一级历史
        goBack() {
            this.$router.go(-1);
        }

    },
}
</script>